<div class="row-fluid">
	<div class="span12">
		<ul class="breadcrumb">
			<li>
				<a href="index.php">
					<i class="radmin-icon radmin-home"></i>
					Dashboard
				</a>
				<span class="divider">/</span>
			</li>
			<li>
				<a href="whm-accounts.php">
					<i class="radmin-icon radmin-clipboard-2"></i>
					WHM Accounts
				</a>
				<span class="divider">/</span>
			</li>
			<li class="active">
				<i class="radmin-icon radmin-loading"></i>
				Restart Services
			</li>
		</ul>
	</div>
</div>

<div class="row-fluid">
	<div class="span12">
		<h4 class="title"><?php $this->widget('application.components.mywidgets.WikiHelp', array('wiki' => 'restart')); ?> Restart Services</h4>
		<div class="squiggly-border"></div>
		<div>
			<?php echo CHtml::dropdownList('id_account', '', $accountsDataSource, array('empty' => 'Restart Server ('.$accountsCount.' Accounts)')); ?><br />
			
			<label class="checkbox">
				<?php echo CHtml::checkBox('httpd', false, array('value' => 1, 'class' => 'services')); ?>
				Apache Web Server
			</label>
			
			<label class="checkbox">
				<?php echo CHtml::checkBox('mysql', false, array('value' => 1, 'class' => 'services')); ?>
				MySQL Server
			</label>
			
			<label class="checkbox">
				<?php echo CHtml::checkBox('sshd', false, array('value' => 1, 'class' => 'services')); ?>
				SSH Daemon
			</label>
			
			<label class="checkbox">
				<?php echo CHtml::checkBox('named', false, array('value' => 1, 'class' => 'services')); ?>
				DNS Server (BIND)
			</label>
			
			<label class="checkbox">
				<?php echo CHtml::checkBox('ftpd', false, array('value' => 1, 'class' => 'services')); ?>
				Ftp Service (ProFTPd/Pure-FTPd)
			</label>
			
			<label class="checkbox">
				<?php echo CHtml::checkBox('exim', false, array('value' => 1, 'class' => 'services')); ?>
				Exim Mail
			</label>
			
			<label class="checkbox">
				<?php echo CHtml::checkBox('imap', false, array('value' => 1, 'class' => 'services')); ?>
				IMAP Server (Courier IMAP/WU IMAP)
			</label>
			
			<label class="checkbox">
				<?php echo CHtml::checkBox('cppop', false, array('value' => 1, 'class' => 'services')); ?>
				POP3 Server (Courier POP3/cppop) 
			</label>
			
			<label class="checkbox">
				<?php echo CHtml::checkBox('accounts_check_all', false, array('value' => 1)); ?>
			</label>
			<script>
				$('#accounts_check_all').click(function(){
					$('.services').removeAttr('checked');
					if($(this).is(":checked")){
						$('.services').attr('checked', true);
					}
				});

				$('.services').live('click', function(){
					$('#accounts_check_all').attr('checked', true);
					$('.services').each(function(k, v){
						if(!$(v).is(":checked")){
						$('#accounts_check_all').removeAttr('checked');
							return true;
						}
					});
				});
			</script>
			
			<button class="btn btn-small btn-primary" id="restart_btn" type="button" data-loading-text="Restarting...">Restart Services</button><br /><br />
			<div class="row-fluid">
				<div class="span12 <?php echo empty($denyRestartAccounts)? 'hide' : ''; ?>" id="restart_info">
					<div class="alert alter-block alert-info">
			<!--			<button type="button" class="close" data-dismiss="alert">&times;</button>-->
						<?php if(!empty($denyRestartAccounts)): ?>
							<h4>IMPORTANT</h4>
							<p>The following WHM Accounts do not have restartservice permission and will most likely fail this request:</p>
							<?php foreach($denyRestartAccounts as $account): ?>
								<p><?php echo $account->domain; ?></p>
							<?php endforeach; ?>
						<?php endif; ?>
						
						<div class="content"></div>
					</div>
				</div>
			</div>
			<script>
				var restartRequest = function(){
					this.requestUrl = '';
					this.id_accounts = null;
					this.services = [];

					this.beforeRequest = function(id_account, doTimes){};
					this.requestCallback = function(id_account, serverReturn, doTimes){};
					this.afterDone = function(){};

					this.doTimes = 0;
					this.do = function(){
						this.doTimes++;
						var id_account = this.id_accounts.pop();
						if(!id_account){
							this.afterDone();
							return false;
						}
						this.beforeRequest(id_account, this.doTimes);
						var thisObj = this;
						$.ajax({
							type: 'post',
							url: thisObj.requestUrl,
//							data: $.extend( 
//								{id_account: id_account},
//								thisObj.services
//							),
							data: {
								id_account: id_account,
								services: thisObj.services
							},
							success: function(ret){
								thisObj.requestCallback(id_account, ret, thisObj.doTimes);
								thisObj.do();
								return true;
							},
							dataType: 'json'
						});
					};
				};
			
				$('#restart_btn').click(function(){
					var id_accounts = [];
					if($('#id_account').val() == ''){
						$('#id_account option[value!=""]').each(function(k, v){
							id_accounts.push($(v).val());
						});
					}else{
						id_accounts.push($('#id_account').val());
					}

					$('#restart_info').blink({
						'beforeCall': function(){
							$('#restart_info .content').append(
								'<p><b>Status ('+id_accounts.length+' Account)</b></p>'
							);
						},
						'callBack': function(){
							var restartRequester = new restartRequest();
							var length = id_accounts.length;
							restartRequester.id_accounts = id_accounts;
							$('.services').each(function(k, v){
								if($(v).is(':checked')){
									restartRequester.services.push($(v).attr('name'));
								}
							});
							if($.isEmptyObject(restartRequester.services)){
								$('#restart_info .content').append(
									'<p>Please select services you want to restart!</p>'
								);
								return false;
							}

							restartRequester.requestUrl = <?php echo json_encode(url('whm/account/restart')); ?>;
							restartRequester.beforeRequest = function(id_account, doTimes){
								$('#restart_btn').button('loading');
								$('#restart_info .content').append(
									'<p>'+$('#id_account option[value="'+id_account+'"]').text()+': Waiting...'+' ('+doTimes+' of '+length+' Accounts)</p>'
								);
							};
							restartRequester.afterDone = function(){
								$('#restart_btn').button('reset');
							}
							restartRequester.requestCallback = function(id_account, serverReturn, doTimes){
								if(serverReturn.result){
									$.each(serverReturn.restart, function(service, detail){
										if(detail.result == 1){
											var result = '<p class="text-success accordion-toggle" data-toggle="collapse" data-target="#'+service+serverReturn.time+'">&nbsp;&nbsp;&nbsp;&nbsp; <b>'+service+' - Success</b> Show/Hide Raw Output</p>';
										}else{
											var result = '<p class="text-error accordion-toggle" data-toggle="collapse" data-target="#'+service+serverReturn.time+'">&nbsp;&nbsp;&nbsp;&nbsp; <b>'+service+' - Failed</b> Show/Hide Raw Output</p>';
										}
										$('#restart_info .content').append(result);
										$('#restart_info .content').append('<p id="'+service+serverReturn.time+'" class="muted collapse out">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <b>'+($.isEmptyObject(detail.rawout)? detail.servicename : detail.rawout)+'</b></p>');
									});
								}else{
									$.each(serverReturn.errors, function(attrName, error){
										$(error).each(function(k, e){
											$('#restart_info .content').append(
												'<p class="text-error">&nbsp;&nbsp;&nbsp;&nbsp; <b>'+$('#id_account option[value="'+id_account+'"]').text()+' - '+e+' ('+doTimes+' of '+length+' Accounts)</b></p>'
											);
										});
									});
								}
							};
							restartRequester.do();
						},
						'showTime': 0
					});
					
				});
			</script>
		</div>
		
	</div>
</div>
